﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Adams.Admin.Presentation.Controllers
{
    [Authorize]
    [EnsureSsl]
    public class ConsoleController : Controller
    {
        [AppendLayoutViewModel]
        public ActionResult Index()
        {
            int userId = User.Identity.Name.ToInt();
            var model = Business.Console.Index.PageLoad(userId);
            return View(model);
        }

        [AppendLayoutViewModel]
        public ActionResult Profile()
        {
            int userId = User.Identity.Name.ToInt();
            var model = Business.Console.Profile.PageLoad(userId);
            ViewBag.Message = Request["msg"];
            return View(model);
        }

        public JsonResult SaveProfile(Adams.Admin.DataTransfer.Console.Profile.FormInput input)
        {
            var model = Business.Console.Profile.SaveProfile(input);
            return Json(model);
        }

        [AppendLayoutViewModel]
        public ActionResult Password()
        {
            int userId = User.Identity.Name.ToInt();
            var model = Business.Console.Password.PageLoad(userId);
            ViewBag.Message = Request["msg"];
            return View(model);
        }

        public JsonResult SavePassword(Adams.Admin.DataTransfer.Console.Password.FormInput input)
        {
            var model = Business.Console.Password.SavePassword(input);
            return Json(model);
        }

        [AppendLayoutViewModel]
        public ActionResult Users()
        {
            
            var model = Business.Console.Users.PageLoad();
            ViewBag.Message = Request["msg"];
            return View(model);
        }

        public JsonResult GridLoad(DataTransfer.GridLoadInput gridinput)
        {
            var model = Business.Console.Users.GridLoad(gridinput);
            return Json(model);
        }

        [AppendLayoutViewModel]
        public ActionResult CreateUser(int id)
        {
            
            var model = Business.Console.CreateUser.PageLoad(id);
            return View(model);
        }

        public JsonResult SaveUser(Adams.Admin.DataTransfer.Console.CreateUser.FormInput input)
        {
            var model = Business.Console.CreateUser.SaveUser(input);
            return Json(model);
        }

        public RedirectResult DeleteUser(int id)
        {
            int userId = User.Identity.Name.ToInt();
            Business.Console.Users.DeleteUser(userId, id);
            var model = new { Deleted = true };
            return Redirect("/Console/Users?msg=2");
        }
    }
}
